package main

import (
	"bufio"
	"fmt"
	"io"
	"learngo/functional/fib"
	"strings"
)

type Fibresult func() int

//给Fibresult添加一个Reader接口,并实现了里面的Read方法，printFileContents方法就可以传入一个Reader接口实现
func (f Fibresult) Read(p []byte) (n int, err error)  {
	next := f()
	if next >20000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", next)
	return strings.NewReader(s).Read(p)
}

func printFileContents(r io.Reader){
	scanner := bufio.NewScanner(r)
	for scanner.Scan(){
		fmt.Println(scanner.Text())
	}
}

func main() {
	var f Fibresult = fib.Fibonacci()
	printFileContents(f)
}
